package com.service;

import com.controller.response.BuildingRentalResponse;
import com.entity.BuildingInfo;
import com.entity.BuildingRentalInfo;
import com.repository.BuildingInfoRepository;
import com.repository.BuildingRentalRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class BuildingRentalInfoService {
    
    @Autowired
    private BuildingRentalRepository buildingRentalRepository;

    @Autowired
    private BuildingInfoRepository buildingInfoRepository;

    public BuildingRentalResponse findByCode(String code) {
        BuildingInfo buildingInfo = buildingInfoRepository.findByBuildingCode(code);
        List<BuildingRentalInfo> buildingRentalInfos = buildingRentalRepository.findByBuildingCodeAndEndDateAfter(code, new Date());

        List<Integer> allFloors = buildingRentalInfos.stream()
                .flatMap(info -> info.getFloors().stream())
                .collect(Collectors.toList());

        return BuildingRentalResponse.builder()
                .id(buildingInfo.getId())
                .code(code)
                .floorCount(buildingInfo.getFloorCount())
                .floors(allFloors)
                .build();
    }

    public BuildingRentalResponse findById(Integer id) {
        BuildingRentalInfo buildingRentalInfo = buildingRentalRepository.findById(id).get();
        BuildingInfo buildingInfo = buildingInfoRepository.findByBuildingCode(buildingRentalInfo.getBuildingCode());
        List<BuildingRentalInfo> buildingRentalInfos = buildingRentalRepository.findByBuildingCodeAndEndDateAfter(buildingInfo.getBuildingCode(), new Date());

        // 排除当前buildingRentalInfo的floors
        List<Integer> allFloors = buildingRentalInfos.stream()
                .filter(info -> !info.getId().equals(id)) // 过滤掉当前ID的记录
                .flatMap(info -> info.getFloors().stream())
                .collect(Collectors.toList());

        return BuildingRentalResponse.builder()
                .id(buildingInfo.getId())
                .code(buildingRentalInfo.getBuildingCode())
                .floorCount(buildingInfo.getFloorCount())
                .floors(allFloors)
                .build();
    }


    /**
     * 创建租赁信息
     * @param buildingRentalInfo 租赁信息
     * @return 保存后的租赁信息
     */
    public BuildingRentalInfo create(BuildingRentalInfo buildingRentalInfo) {
        buildingRentalInfo.setCreateTime(new Date());
        buildingRentalInfo.setUpdateTime(new Date());
        return buildingRentalRepository.save(buildingRentalInfo);
    }
    
    /**
     * 根据ID删除租赁信息
     * @param id 租赁信息ID
     */
    public void deleteById(Integer id) {
        buildingRentalRepository.deleteById(id);
    }
    
    /**
     * 更新租赁信息
     * @param buildingRentalInfo 租赁信息
     * @return 更新后的租赁信息
     */
    public BuildingRentalInfo update(BuildingRentalInfo buildingRentalInfo) {
        buildingRentalInfo.setUpdateTime(new Date());
        return buildingRentalRepository.save(buildingRentalInfo);
    }
    
    /**
     * 分页查询租赁信息
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 租赁信息分页结果
     */
    public Page<BuildingRentalInfo> findAll(int pageNum, int pageSize) {
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, Sort.by(Sort.Direction.DESC, "createTime"));
        return buildingRentalRepository.findAll(pageable);
    }

    /**
     * 获取所有租赁信息中的楼层列表并整合成一个List
     * @return 所有楼层的整合列表
     */
    public List<Integer> getAllFloorsAsList() {
        List<BuildingRentalInfo> buildingRentalInfos = buildingRentalRepository.findAll();
        return buildingRentalInfos.stream()
                .flatMap(info -> info.getFloors().stream())
                .collect(Collectors.toList());
    }
    
    /**
     * 根据建筑代码获取租赁信息中的楼层列表并整合成一个List
     * @param buildingCode 建筑代码
     * @return 楼层的整合列表
     */
    public List<Integer> getFloorsByBuildingCode(String buildingCode) {
        List<BuildingRentalInfo> buildingRentalInfos = buildingRentalRepository.findByBuildingCodeAndEndDateAfter(buildingCode, new Date());
        return buildingRentalInfos.stream()
                .flatMap(info -> info.getFloors().stream())
                .collect(Collectors.toList());
    }
}